import * as api from './api';
import {PAGE_CODE} from './constants';
import {PAGE} from '../constants';
import {
    getCurrentUserPagePermissionsSuccess,
    getCurrentUserPagePermissionsFail,
    getCurrentUserPagePermissionsApi
} from 'Shared/actions/currentUserContext';
export const ERROR_CODE = 'error';
export const GET_INIT_DATA_BEGIN = 'GET_INIT_DATA_BEGIN';
export const GET_INIT_DATA_SUCCESS = 'GET_INIT_DATA_SUCCESS';
export const GET_INIT_DATA_FAIL = 'GET_INIT_DATA_FAIL';
import {conventTableCondition} from '../utils';
import moment from 'moment';
import {claimStatus} from '../Enum';

const getInitDataBegin = () => ({
    type: GET_INIT_DATA_BEGIN
});

const getInitDataSuccess = data => ({
    type: GET_INIT_DATA_SUCCESS,
    data
});
const getInitDataFail = () => ({
    type: GET_INIT_DATA_FAIL
});
/**初始化 */
/*==============================查询列表================================= */
export const GET_LIST_BEGIN = 'GET_LIST_BEGIN';
export const GET_LIST_SUCCESS = 'GET_LIST_SUCCESS';
export const GET_LIST_FAIL = 'GET_LIST_FAIL';
export const getListBegin = () => ({
    type: GET_LIST_BEGIN
});
const getListSuccess = (condition, data) => ({
    type: GET_LIST_SUCCESS,
    condition,
    data
});
export const getListFail = () => ({
    type: GET_LIST_FAIL
});

/*==============================初始化车型================================= */
export const GET_INIT_CATEGORY_DATA_BEGIN = 'GET_INIT_CATEGORY_DATA_BEGIN';
export const GET_INIT_CATEGORY_DATA_SUCCESS = 'GET_INIT_CATEGORY_DATA_SUCCESS';
export const GET_INIT_CATEGORY_DATA_FAIL = 'GET_INIT_CATEGORY_DATA_FAIL';
const getInitCategoryDataBegin = () => ({
    type: GET_INIT_CATEGORY_DATA_BEGIN
});

const getInitCategoryDataSuccess = data => ({
    type: GET_INIT_CATEGORY_DATA_SUCCESS,
    data
});
const getInitCategoryDataFail = () => ({
    type: GET_INIT_CATEGORY_DATA_FAIL
});
/*==============================查询action================================= */
const conventDate = (condition = {}) => {
    const {createTime, ...others} = condition;
    if(!createTime || createTime.length === 0) return condition;
    return {
        ...others,
        beginCreateTime: moment(createTime[0]).toISOString(),
        endCreateTime: moment(createTime[1]).toISOString(),
    };
};
const submitTimeDate = (condition = {}) => {
    const {submitTime, ...others} = condition;
    if(!submitTime || submitTime.length === 0) return condition;
    return {
        ...others,
        beginSubmitTime: moment(submitTime[0]).toISOString(),
        endSubmitTime: moment(submitTime[1]).toISOString(),
    };
};
const approveTimeDate = (condition = {}) => {
    const {approveTime, ...others} = condition;
    if(!approveTime || approveTime.length === 0) return condition;
    return {
        ...others,
        beginApproveTime: moment(approveTime[0]).toISOString(),
        endApproveTime: moment(approveTime[1]).toISOString(),
    };
};
const adjustedTimeDate = (condition = {}) => {
    const {adjustedTime, ...others} = condition;
    if(!adjustedTime || adjustedTime.length === 0) return condition;
    return {
        ...others,
        beginAdjustedTime: moment(adjustedTime[0]).toISOString(),
        endAdjustedTime: moment(adjustedTime[1]).toISOString(),
    };
};
//获取查询列表
export const getList = condition => dispatch => {
    dispatch(getListBegin());
    const queryCondition = adjustedTimeDate(approveTimeDate(submitTimeDate(conventDate(conventTableCondition(condition)))));
    return api.getList(queryCondition).then(res => {
        if(res.ok)
            dispatch(getListSuccess(condition, res.data));
        else
            dispatch(getListFail());
    });
};
/*==============================初始化API================================= */
//获取初始化数据和权限
export const getInitData = () => dispatch => {
    dispatch(getInitDataBegin());
    const init = api.getInitData().then(res => {
        if(res.ok) {
            dispatch(getInitDataSuccess(res.data));
            if(!res.data.isDealerUser)
                dispatch(getList({
                    status: claimStatus.待审核,
                    pageSize: PAGE.size
                }));
        } else
            dispatch(getInitDataFail());
        return res.ok;
    });
    dispatch(getInitCategoryDataBegin());
    const category = api.productCategoryQuery().then(res => {
        if(res.ok)
            dispatch(getInitCategoryDataSuccess(res.data));
        else
            dispatch(getInitCategoryDataFail());
        return res.ok;
    });
    const permissions = getCurrentUserPagePermissionsApi(PAGE_CODE).then(res => {
        if(res.isOk) dispatch(getCurrentUserPagePermissionsSuccess(res.data));
        else dispatch(getCurrentUserPagePermissionsFail(res.statusCode, res.message));
        return res.isOk;
    });

    return Promise.all([init, permissions, category]).then(values => values[0] && values[1] && values[2]);
};

//修改查询条件
export const SAVE_QUERY_CONDITION = 'SAVE_QUERY_CONDITION';
export const saveQueryCondition = (name, value) => ({
    type: SAVE_QUERY_CONDITION,
    name,
    value
});
//重置查询条件
export const RESET_QUERY_PANEL = 'RESET_QUERY_PANEL';
export const resetQueryPanel = () => ({
    type: RESET_QUERY_PANEL,
});

//按钮搜索
export const querySearch = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    const pageTableCondition = getState().getIn(['page', 'appState', 'queryBySort']).toJS();
    condition.pageSize = pageTableCondition.pageSize || PAGE.size;
    condition.sortedColumnKey = pageTableCondition.sortedColumnKey;
    condition.sortedOrder = pageTableCondition.sortedOrder;
    dispatch(getList(condition));
};
//分页排序搜索
export const tableSearch = page => (dispatch, getState) => {
    const query = getState().getIn(['page', 'appState', 'queryBySort']).toJS();
    if(!query.pageIndex || !page)
        query.pageIndex = 0;
    const condition = Object.assign({}, query, page);
    dispatch(getList(condition));
};

//选择查询条件
export const SAVE_QUERY_DEALER = 'SAVE_QUERY_DEALER';
export const saveQueryDealer = data => ({
    type: SAVE_QUERY_DEALER,
    data
});
export const SAVE_QUERY_PARTCATEGORY = 'SAVE_QUERY_PARTCATEGORY';
export const saveQueryPartCategory = data => ({
    type: SAVE_QUERY_PARTCATEGORY,
    data
});
export const SAVE_QUERY_FAULTITEM = 'SAVE_QUERY_FAULTITEM';
export const saveQueryFaultItem = data => ({
    type: SAVE_QUERY_FAULTITEM,
    data
});

/*==============================新增修改action================================= */
export const onSubmit = (data, details, claimWorkItemDetails, claimMaterialDetails, claimAthDetails, isSubmit) => () => {
    const addCondition = {
        subDealerId: data.subDealerId,
        brandId: data.brandId,
        claimApplyId: data.claimApplyId,
        subClaimId: data.subClaimId,
        subClaimCode: data.subClaimCode,
        repairType: data.repairType,
        faultModeId: data.faultModeId,
        faultDescription: data.faultDescription,
        isOut: data.isOut,
        outFee: data.outFee,
        supplierId: data.supplierId,
        supplierCode: data.supplierCode,
        partPurchasedDate: data.partPurchasedDate,
        vehicleSoldId: data.vehicleSoldId,
        repairCode: data.repairCode,
        repairDate: data.repairDate,
        isOtherArea: data.isOtherArea,
        authorizedCode: data.authorizedCode,
        faultType: data.faultType,
        faultItemId: data.faultItemId,
        faultItemName: data.faultItemName,
        checkResul: data.checkResul,
        checkApproveComment: data.checkApproveComment,
        faultModeName: data.faultModeName,
        mileage: data.mileage ? data.mileage : 0,
        engineCode: data.engineCode,
        transmissionSn: data.transmissionSn,
        details: details.map(v => ({
            usedPartId: v.usedPartId,
            usedQty: v.usedQty,
            isAccessory: true,
            accessorySource: v.accessorySource
        })),
        claimWorkItemDetails: claimWorkItemDetails.map(v => ({
            usedPartId: v.usedPartId,
            workItemId: v.workItemId,
            isDefault: v.isDefault,
            laborHour: v.laborHour
        })),
        claimMaterialDetails: claimMaterialDetails.map(v => ({
            isCaused: v.isCaused,
            usedPartId: v.usedPartId,
            newPartId: v.newPartId,
            quantity: v.quantity,
            isAccessory: false
        })),
        claimAthDetails: claimAthDetails.map(v => ({
            type: v.type,
            fileType: v.fileType,
            attachments: v.attachments === undefined ? '' : v.attachments.map(o => ({
                fileId: o.fileId,
                fileName: o.fileName
            }))
        }))
    };
    const updateCondition = {
        subDealerId: data.subDealerId,
        message: data.message,
        rowVersion: data.rowVersion,
        claimApplyId: data.claimApplyId,
        faultDescription: data.faultDescription,
        isOut: data.isOut,
        outFee: data.outFee,
        supplierId: data.supplierId,
        supplierCode: data.supplierCode,
        partPurchasedDate: data.partPurchasedDate,
        repairCode: data.repairCode,
        repairDate: data.repairDate,
        authorizedCode: data.authorizedCode,
        checkResul: data.checkResul,
        checkApproveComment: data.checkApproveComment,
        mileage: data.mileage ? data.mileage : 0,
        engineCode: data.engineCode,
        transmissionSn: data.transmissionSn,
        faultType: data.faultType,
        repairType: data.repairType,
        details: details.map(v => ({
            usedPartId: v.usedPartId,
            usedQty: v.usedQty,
            isAccessory: true,
            accessorySource: v.accessorySource
        })),
        claimWorkItemDetails: claimWorkItemDetails.map(v => ({
            usedPartId: v.usedPartId,
            workItemId: v.workItemId,
            isDefault: v.isDefault,
            laborHour: v.laborHour
        })),
        claimMaterialDetails: claimMaterialDetails.map(v => ({
            isCaused: v.isCaused,
            usedPartId: v.usedPartId,
            newPartId: v.newPartId,
            quantity: v.quantity,
            isAccessory: false
        })),
        claimAthDetails: claimAthDetails.map(v => ({
            type: v.type,
            fileType: v.fileType,
            attachments: v.attachments === undefined ? '' : v.attachments.map(o => ({
                fileId: o.fileId,
                fileName: o.fileName
            }))
        }))
    };
    //新增修改并提交
    if(isSubmit)
        return data.id ? api.modifyAndSubmitOrder(data.id, updateCondition) : api.newAndSubmitOrder(addCondition);
    return data.id ? api.modifyOrder(data.id, updateCondition) : api.newOrder(addCondition);
};

/*==============================审核action================================= */
export const onApprove = (data, details, claimMaterialDetails, claimWorkItemDetails) => () => {
    const condition = {
        rowVersion: data.rowVersion,
        dutyUnitId: data.dutyUnitId,
        approveComment: data.approveComment,
        approvedResult: data.approvedResult,
        supplierId: data.supplierId,
        engineCode: data.engineCode,
        transmissionSn: data.transmissionSn,
        claimSigned: data.claimSigned,
        claimSignedRemark: data.claimSignedRemark,
        isReplacedDutyUnit: data.isReplacedDutyUnit,
        isDutyUnitWarranty: data.isDutyUnitWarranty,
        isEngineCode: data.isEngineCode,
        istransmissionModel: data.istransmissionModel,
        determinedSupplierId: data.determinedSupplierId,
        details: details.map(v => ({
            usedPartId: v.usedPartId,
            usedQty: v.usedQty,
            isAccessory: true,
            accessorySource: v.accessorySource
        })),
        claimWorkItemDetails: claimWorkItemDetails.map(v => ({
            usedPartId: v.usedPartId,
            workItemId: v.workItemId,
            isDefault: v.isDefault,
            laborHour: v.laborHour,
            laborPrice: v.laborPrice
        })),
        claimMaterialDetails: claimMaterialDetails.map(v => ({
            isCaused: v.isCaused,
            usedPartId: v.usedPartId,
            newPartId: v.newPartId,
            quantity: v.quantity,
            isAccessory: false
        }))
    };
    return api.approveData(data.id, condition).then(res => ({success: res.ok}));
};
export const onReject = (data, details, claimMaterialDetails, claimWorkItemDetails) => () => {
    const condition = {
        rowVersion: data.rowVersion,
        dutyUnitId: data.dutyUnitId,
        approveComment: data.approveComment,
        approvedResult: data.approvedResult,
        supplierId: data.supplierId,
        engineCode: data.engineCode,
        transmissionSn: data.transmissionSn,
        claimSigned: data.claimSigned,
        claimSignedRemark: data.claimSignedRemark,
        determinedSupplierId: data.determinedSupplierId,
        details: details.map(v => ({
            usedPartId: v.usedPartId,
            usedQty: v.usedQty,
            isAccessory: true,
            accessorySource: v.accessorySource
        })),
        claimWorkItemDetails: claimWorkItemDetails.map(v => ({
            usedPartId: v.usedPartId,
            workItemId: v.workItemId,
            isDefault: v.isDefault,
            laborHour: v.laborHour,
            laborPrice: v.laborPrice
        })),
        claimMaterialDetails: claimMaterialDetails.map(v => ({
            isCaused: v.isCaused,
            usedPartId: v.usedPartId,
            newPartId: v.newPartId,
            quantity: v.quantity,
            isAccessory: false
        }))
    };
    return api.rejectData(data.id, condition).then(res => ({success: res.ok}));
};

export const onRefuse = (data, details, claimMaterialDetails, claimWorkItemDetails) => () => {
    const condition = {
        rowVersion: data.rowVersion,
        dutyUnitId: data.dutyUnitId,
        approveComment: data.approveComment,
        approvedResult: data.approvedResult,
        supplierId: data.supplierId,
        engineCode: data.engineCode,
        transmissionSn: data.transmissionSn,
        claimSigned: data.claimSigned,
        claimSignedRemark: data.claimSignedRemark,
        determinedSupplierId: data.determinedSupplierId,
        details: details.map(v => ({
            usedPartId: v.usedPartId,
            usedQty: v.usedQty,
            isAccessory: true,
            accessorySource: v.accessorySource
        })),
        claimWorkItemDetails: claimWorkItemDetails.map(v => ({
            usedPartId: v.usedPartId,
            workItemId: v.workItemId,
            isDefault: v.isDefault,
            laborHour: v.laborHour,
            laborPrice: v.laborPrice
        })),
        claimMaterialDetails: claimMaterialDetails.map(v => ({
            isCaused: v.isCaused,
            usedPartId: v.usedPartId,
            newPartId: v.newPartId,
            quantity: v.quantity,
            isAccessory: false
        }))
    };
    return api.refuseData(data.id, condition).then(res => ({success: res.ok}));
};
//=====================================调整==============================
export const onAdjust = (data, details, claimMaterialDetails, claimWorkItemDetails) => () => {
    const condition = {
        rowVersion: data.rowVersion,
        supplierId: data.supplierId,
        engineCode: data.engineCode,
        transmissionSn: data.transmissionSn,
        claimSigned: data.claimSigned,
        claimSignedRemark: data.claimSignedRemark,
        approveComment: data.approveComment,
        approvedResult: data.approvedResult,
        isReplacedDutyUnit: data.isReplacedDutyUnit,
        isDutyUnitWarranty: data.isDutyUnitWarranty,
        isEngineCode: data.isEngineCode,
        istransmissionModel: data.istransmissionModel,
        dutyUnitId: data.dutyUnitId,
        determinedSupplierId: data.determinedSupplierId,
        details: details.map(v => ({
            usedPartId: v.usedPartId,
            usedQty: v.usedQty,
            isAccessory: true,
            accessorySource: v.accessorySource
        })),
        claimWorkItemDetails: claimWorkItemDetails.map(v => ({
            usedPartId: v.usedPartId,
            workItemId: v.workItemId,
            laborHour: v.laborHour,
            laborPrice: v.laborPrice,
            isDefault: v.isDefault
        })),
        claimMaterialDetails: claimMaterialDetails.map(v => ({
            isCaused: v.isCaused,
            usedPartId: v.usedPartId,
            newPartId: v.newPartId,
            quantity: v.quantity,
            isAccessory: false
        }))
    };
    return api.adjustData(data.id, condition).then(res => ({success: res.ok}));
};
//=====================================上传附件==============================
export const ADD_FILE = 'ADD_FILE';
export const addFiles = data => ({
    type: ADD_FILE,
    data: {
        fileId: data.id,
        fileName: data.fileName
    }
});

export const REMOVE_FILE = 'REMOVE_FILE';
export const removeFile = id => ({
    type: REMOVE_FILE,
    id
});

export const CLEAR_DATA = 'CLEAR_DATA';
export const clearData = () => ({
    type: CLEAR_DATA
});

export const onUpload = () => (dispatch, getState) => {
    const deta = getState().getIn(['page', 'domainData', 'athFile']).toJS();
    const condition = {
        attachments: deta.attachments.map(v => ({
            fileId: v.fileId,
            fileName: v.fileName
        }))
    };
    return api.uploadInfo(condition).then(res => ({success: res.ok}));
};

//=====================================历史查询==============================
export const GET_HISTORY_LIST_BEGIN = 'GET_HISTORY_LIST_BEGIN';
export const GET_HISTORY_LIST_SUCCESS = 'GET_HISTORY_LIST_SUCCESS';
export const GET_HISTORY_LIST_FAIL = 'GET_HISTORY_LIST_FAIL';
export const getHistoryListBegin = () => ({
    type: GET_HISTORY_LIST_BEGIN
});
export const getHistoryListSuccess = (data, condition) => ({
    type: GET_HISTORY_LIST_SUCCESS,
    data,
    condition
});
export const getHistoryListFail = () => ({
    type: GET_HISTORY_LIST_FAIL,
});

//获取列表
export const getHistoryList = condition => dispatch => {
    dispatch(getHistoryListBegin());
    const {createTime, dealerCode, dealerName, ...props} = condition;
    if(createTime && createTime.length > 0) {
        props.beginCreateTime = moment(createTime[0]).toISOString();
        props.endCreateTime = moment(createTime[1]).toISOString();
    }
    return api.getHistoryList(props).then(res => {
        if(res.ok)
            dispatch(getHistoryListSuccess(res.data, condition));
        else
            dispatch(getHistoryListFail());
        return res.ok;
    });
};

//按钮搜索历史
export const queryHistorySearch = () => (dispatch, getState) => {
    const condition = getState()
        .getIn(['page', 'appState', 'historyQueryCondition'])
        .toJS();
    return dispatch(getHistoryList(condition));
};

export const tableHistorySearch = page => (dispatch, getState) => {
    const query = getState()
        .getIn(['page', 'appState', 'historyQueryBySort'])
        .toJS();
    const condition = Object.assign({}, query, page);
    return dispatch(getHistoryList(condition));
};

//修改历史查询条件
export const SAVE_HISTORY_QUERY_CONDITION = 'SAVE_HISTORY_QUERY_CONDITION';
export const saveHistoryQueryCondition = (name, value) => ({
    type: SAVE_HISTORY_QUERY_CONDITION,
    name,
    value
});

//重置历史查询条件
export const RESET_HISTORY_QUERY_PANEL = 'RESET_HISTORY_QUERY_PANEL';
export const resetHistoryQueryPanel = () => ({
    type: RESET_HISTORY_QUERY_PANEL,
});

export const SELECT_HISTORY_DEALER_QUERY = 'SELECT_HISTORY_DEALER_QUERY';
export const selectHistoryDealer = data => ({
    type: SELECT_HISTORY_DEALER_QUERY,
    data
});
/*==============================导入模板action================================= */
export const claimImportTemp = () => () => api.claimImportTemp();

//批量打印旧件标签
export const START_BATCH_PRINT = 'START_BATCH_PRINT';
export const startBatchPrint = () => ({
    type: START_BATCH_PRINT
});
export const CLEAR_BATCH_DATA = 'CLEAR_BATCH_DATA';
export const clearBatchData = () => ({
    type: CLEAR_BATCH_DATA,
});
/*==============================查询导出action================================= */
export const exportData = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    const {pageIndex, pageSize, sortField, isDesc, ...others} = condition;
    return api.queryExport(adjustedTimeDate(approveTimeDate(submitTimeDate(conventDate(conventTableCondition(others))))));
};

export const exportHistoryData = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'historyQueryCondition']).toJS();
    const {createTime, dealerCode, dealerName, pageIndex, pageSize, sortField, isDesc, ...props} = condition;
    if(createTime && createTime.length > 0) {
        props.beginCreateTime = moment(createTime[0]).toISOString();
        props.endCreateTime = moment(createTime[1]).toISOString();
    }
    return api.queryHistoryExport(conventTableCondition(props));
};
/*==============================查询责任单位================================= */
export const GET_DUTYUNIT_BEGIN = 'GET_DUTYUNIT_BEGIN';
export const GET_DUTYUNIT_SUCCESS = 'GET_DUTYUNIT_SUCCESS';
export const GET_DUTYUNIT_FAIL = 'GET_DUTYUNIT_FAIL';
export const getDutyUnitListBegin = () => ({
    type: GET_DUTYUNIT_BEGIN
});
const getDutyUnitListSuccess = data => ({
    type: GET_DUTYUNIT_SUCCESS,
    data
});
export const getDutyUnitListFail = () => ({
    type: GET_DUTYUNIT_FAIL
});
//获取列表
export const getDutyUnitList = () => dispatch => {
    dispatch(getDutyUnitListBegin());
    return api.getDutyUnitLists().then(res => {
        if(res.ok)
            dispatch(getDutyUnitListSuccess(res.data));
        else
            dispatch(getDutyUnitListFail());
        return res.ok;
    });
};

/*==============================查询自动审核日志================================= */
export const GET_AUTO_LIST_BEGIN = 'GET_AUTO_LIST_BEGIN';
export const GET_AUTO_LIST_SUCCESS = 'GET_AUTO_LIST_SUCCESS';
export const GET_AUTO_LIST_FAIL = 'GET_AUTO_LIST_FAIL';
export const getAutoListBegin = () => ({
    type: GET_AUTO_LIST_BEGIN
});
export const getAutoListSuccess = (data, condition) => ({
    type: GET_AUTO_LIST_SUCCESS,
    data,
    condition
});
export const getAutoListFail = () => ({
    type: GET_AUTO_LIST_FAIL,
});

//获取列表
export const autoList = condition => dispatch => {
    dispatch(getAutoListBegin());
    return api.getAutoList(condition).then(res => {
        if(res.ok)
            dispatch(getAutoListSuccess(res.data, condition));
        else
            dispatch(getAutoListFail());
        return res.ok;
    });
};

//初始查询
export const getAutoList = () => (dispatch, getState) => {
    const condition = getState()
        .getIn(['page', 'appState', 'autoQueryCondition'])
        .toJS();
    return dispatch(autoList(condition));
};

export const tableAutoSearch = page => (dispatch, getState) => {
    const query = getState()
        .getIn(['page', 'appState', 'autoQueryBySort'])
        .toJS();
    const condition = Object.assign({}, query, page);
    return dispatch(autoList(condition));
};


/*==============================批量审核action================================= */
export const onApproveAll = (data, details, claimMaterialDetails, claimWorkItemDetails) => () => {
    const condition = {
        dutyUnitId: data.dutyUnitId,
        approveComment: data.approveComment,
        approvedResult: data.approvedResult,
        claimSigned: data.claimSigned,
        claimSignedRemark: data.claimSignedRemark,
        isReplacedDutyUnit: data.isReplacedDutyUnit,
        isEngineCode: data.isEngineCode,
        determinedSupplierId: data.determinedSupplierId,
        claims: data.claims,
        details: details.map(v => ({
            usedPartId: v.usedPartId,
            usedQty: v.usedQty,
            isAccessory: true,
            accessorySource: v.accessorySource
        })),
        claimWorkItemDetails: claimWorkItemDetails.map(v => ({
            usedPartId: v.usedPartId,
            workItemId: v.workItemId,
            isDefault: v.isDefault,
            laborHour: v.laborHour,
            laborPrice: v.laborPrice
        })),
        claimMaterialDetails: claimMaterialDetails.map(v => ({
            isCaused: v.isCaused,
            usedPartId: v.usedPartId,
            newPartId: v.newPartId,
            quantity: v.quantity,
            isAccessory: false
        }))
    };
    return api.approveAllData(condition).then(res => res);
};
export const onRejectAll = (data, details, claimMaterialDetails, claimWorkItemDetails) => () => {
    const condition = {
        dutyUnitId: data.dutyUnitId,
        approveComment: data.approveComment,
        approvedResult: data.approvedResult,
        claimSigned: data.claimSigned,
        claimSignedRemark: data.claimSignedRemark,
        determinedSupplierId: data.determinedSupplierId,
        claims: data.claims,
        details: details.map(v => ({
            usedPartId: v.usedPartId,
            usedQty: v.usedQty,
            isAccessory: true,
            accessorySource: v.accessorySource
        })),
        claimWorkItemDetails: claimWorkItemDetails.map(v => ({
            usedPartId: v.usedPartId,
            workItemId: v.workItemId,
            isDefault: v.isDefault,
            laborHour: v.laborHour,
            laborPrice: v.laborPrice
        })),
        claimMaterialDetails: claimMaterialDetails.map(v => ({
            isCaused: v.isCaused,
            usedPartId: v.usedPartId,
            newPartId: v.newPartId,
            quantity: v.quantity,
            isAccessory: false
        }))
    };
    return api.rejectAllData(condition).then(res => res);
};

export const onRefuseAll = (data, details, claimMaterialDetails, claimWorkItemDetails) => () => {
    const condition = {
        dutyUnitId: data.dutyUnitId,
        approveComment: data.approveComment,
        approvedResult: data.approvedResult,
        claimSigned: data.claimSigned,
        claimSignedRemark: data.claimSignedRemark,
        determinedSupplierId: data.determinedSupplierId,
        claims: data.claims,
        details: details.map(v => ({
            usedPartId: v.usedPartId,
            usedQty: v.usedQty,
            isAccessory: true,
            accessorySource: v.accessorySource
        })),
        claimWorkItemDetails: claimWorkItemDetails.map(v => ({
            usedPartId: v.usedPartId,
            workItemId: v.workItemId,
            isDefault: v.isDefault,
            laborHour: v.laborHour,
            laborPrice: v.laborPrice
        })),
        claimMaterialDetails: claimMaterialDetails.map(v => ({
            isCaused: v.isCaused,
            usedPartId: v.usedPartId,
            newPartId: v.newPartId,
            quantity: v.quantity,
            isAccessory: false
        }))
    };
    return api.refuseAllData(condition).then(res => res);
};
/*==============================结算导出action================================= */
export const settlementExportData = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    const {pageIndex, pageSize, sortField, isDesc, ...others} = condition;
    return api.settlementExport(adjustedTimeDate(approveTimeDate(submitTimeDate(conventDate(conventTableCondition(others))))));
};

//图片预览翻页
export const UPDATE_SHOWIMG = 'UPDATE_SHOWIMG';
export const updateIndex = data => ({
    type: UPDATE_SHOWIMG,
    data
});

//批量下载
export const START_DOWNLOAD_OPTION = 'START_DOWNLOAD_OPTION';
export const startDownload = () => ({
    type: START_DOWNLOAD_OPTION
});
export const CLEAR_DOWNLOAD_OPTION = 'CLEAR_DOWNLOAD_OPTION';
export const clearDownload = () => ({
    type: CLEAR_DOWNLOAD_OPTION,
});
